const {RZsnmp,generateSnmpCMD,createCMD} = require('./next');
const {getConnectParam} = require('../common')
const snmp = require('net-snmp');
const ObjectType = require('./PDUObjectType')
const cloudOltURL = 'http://192.168.10.22:23'
function generateCMD(ip,port,oids){
    if(!ip){
        console.error('参数ip地址异常',ip)
        return;
    }
    if(!port){
        console.error('参数port异常',port)
        return;
    }
    if(!oids||oids.length===0){
        console.error('参数oids异常',oids)
        return;
    }
    if(!Array.isArray(oids)){
        oids = [oids];        
    }
    const commands = []
    oids.forEach(oid => {
        if(typeof oid ==='string') {
            commands.push({
                out: true,
                expect: /.*\$$/ig,
                send: `oltsnmp ${ip} ${port} ${oid}`
            },)
        }
    });
    let length = commands.length;
    //获取snmp信息为短连接，所以最后一句命令是exit
    if(length>0&&commands[length-1].out){
        commands.push({
            out: false,
            expect: /.*\$$/ig,
            send: 'exit',
        })
    }
    return commands;
}
function getValue(buffer,type){
    if(type===67){
        return parseInt(buffer.toString());
    }
    if(type===4){
        return buffer.toString();
    }
    return null;
}
async function createSession(ip,community,options){
    let target = await getConnectParam('snmp',ip,this);
    if(!target){
        reject("getConnectParam return null")
    }
    let {tid, ip:destIP, proxyFlag} = target;
    let [host,port] = destIP.split(':');
    ip=host;
    options.port=port
    return snmp.createSession( ip,community,options );
}

module.exports = {
    createSession
}